/**
 * 
 */
package fr.aprr.geoloc.client.view;


import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuBar;
import com.sencha.gxt.widget.core.client.menu.MenuBarItem;
import com.sencha.gxt.widget.core.client.menu.MenuItem;

import fr.aprr.geoloc.client.presenser.interfaces.IMenuView;
import fr.aprr.geoloc.client.presenser.interfaces.IMenuView.IMenuPresenter;
import fr.aprr.geoloc.client.service.OngletsServices;
import fr.aprr.geoloc.client.service.OngletsServicesAsync;
import fr.aprr.geoloc.shared.domain.Onglets;

/**
 * @author PHAM
 *
 */
public class MenuView extends ReverseCompositeView<IMenuPresenter> implements
		IMenuView {
	
	private final OngletsServicesAsync ongletService = GWT
			.create(OngletsServices.class);

	
	
	Menu affConfig = new Menu();
	
	public MenuView() {

		// Event
		SelectionHandler<Item> hander = new SelectionHandler<Item>() {

			public void onSelection(SelectionEvent<Item> event) {
				// TODO Auto-generated method stub
				if (event.getSelectedItem() instanceof MenuItem) {
					MenuItem itemSelect = (MenuItem) event.getSelectedItem();
					Info.display("Action",
							"You select action " + itemSelect.getText());
					presenter.addNewTab(itemSelect.getText());
				}
			}
		};

		// Initial
		MenuItem infoItem = new MenuItem("Info");
		MenuItem exitItem = new MenuItem("Exit");
		Menu menuFile = new Menu();
		menuFile.add(infoItem);
		menuFile.add(exitItem);

		MenuItem confiAppItem = new MenuItem("Configuration de l'application");
		MenuItem confiPassItem = new MenuItem("Configuration barre de passage");
		Menu menuConfig = new Menu();
		menuConfig.add(confiAppItem);
		menuConfig.add(confiPassItem);

		
		
		// Get Onglets
		ongletService.getAllOnglets(new AsyncCallback<List<Onglets>>() {
			
			public void onSuccess(List<Onglets> results) {
				// TODO Auto-generated method stub
				for (Onglets onglets : results) {
					MenuItem parisItem = new MenuItem(onglets.getNomOnglet());
					affConfig.add(parisItem);
				}
			}
			
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				Info.display("Menu", "Get Onglet Erreur");
			}
		});
		
		// add Handler
		affConfig.addSelectionHandler(hander);

		MenuItem archireItem = new MenuItem("Change archive");
		Menu archireConfig = new Menu();
		archireConfig.add(archireItem);

		MenuBar menuBar = new MenuBar();

		menuBar.add(new MenuBarItem("File", menuFile));

		menuBar.add(new MenuBarItem("Configuration", menuConfig));

		menuBar.add(new MenuBarItem("Affichage", affConfig));

		menuBar.add(new MenuBarItem("Archive", archireConfig));

		initWidget(menuBar);
	}

}
